package cn.com.lms.business.livecourse.service;

import com.aliyuncs.vod.model.v20170321.*;
import org.springframework.stereotype.Service;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AliVodService
 *
 * @author barry
 * @date 2020/3/17
 **/
@Service
public class AliVodService {
    public static final String ACCESS_KEY_ID = "LTAIawyT4";
    public static final String ACCESS_KEY_SECRET = "4uttvPctr2b";

    public static DefaultAcsClient initVodClient() throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, AliVodService.ACCESS_KEY_ID, AliVodService.ACCESS_KEY_SECRET);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /*获取播放地址函数*/
    public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client, String vid) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(vid);
        return client.getAcsResponse(request);
    }

    /**
     * 获取点播视频播放url
     * @param vid
     * @return
     * @throws ClientException
     */
    public String vodPlayUrl(String vid) throws ClientException {
        String vodPlayUrl = "";
        DefaultAcsClient client = initVodClient();
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        try {
            response = getPlayInfo(client, vid);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                vodPlayUrl = playInfo.getPlayURL();
                break;
            }
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        return vodPlayUrl;
    }

    /**
     * 获取点播视频的基本信息
     * @param vid
     * @return
     * @throws ClientException
     */
    public GetPlayInfoResponse.VideoBase vodBaseInfo(String vid) throws ClientException {
        DefaultAcsClient client = initVodClient();
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        try {
            response = getPlayInfo(client, vid);
//            //Base信息
//            System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
//            System.out.print("VideoBase.Duration = " + response.getVideoBase().getDuration() + "\n");
//            System.out.print("VideoBase.CoverUrl = " + response.getVideoBase().getCoverURL() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
            return null;
        }
        return response.getVideoBase();
    }

    public void vodInfo(String vid) throws ClientException {
        DefaultAcsClient client = initVodClient();
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        try {
            response = getPlayInfo(client, vid);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
            }
            //Base信息
            System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
            System.out.print("VideoBase.Duration = " + response.getVideoBase().getDuration() + "\n");
            System.out.print("VideoBase.CoverUrl = " + response.getVideoBase().getCoverURL() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }

    /*获取播放凭证函数*/
    public static GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client, String vid) throws Exception {
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(vid);
        return client.getAcsResponse(request);
    }

    /**
     * 获取播放凭证
     * @param vid
     * @throws ClientException
     */
    public void vodAuth(String vid) throws ClientException {
        DefaultAcsClient client = initVodClient();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        try {
            response = getVideoPlayAuth(client, vid);
            //播放凭证
            System.out.print("PlayAuth = " + response.getPlayAuth() + "\n");
            //VideoMeta信息
            System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }

    /**
     * 获取源文件信息
     * @throws Exception
     */
    public Map<String, Object> getMezzanineInfo(String vid) {
        GetMezzanineInfoResponse response = null;
        try {
            DefaultAcsClient client = initVodClient();
            GetMezzanineInfoRequest request = new GetMezzanineInfoRequest();
            request.setVideoId(vid);
    //        // 源片下载地址过期时间
    //        request.setAuthTimeout(3600L);
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
//            e.printStackTrace();
            return null;
        }
        System.out.print("Mezzanine.Duration = " + response.getMezzanine().getDuration() + "\n");
//        System.out.print("Mezzanine.VideoId = " + response.getMezzanine().getVideoId() + "\n");
//        System.out.print("Mezzanine.FileURL = " + response.getMezzanine().getFileURL() + "\n");
//        System.out.print("Mezzanine.Width = " + response.getMezzanine().getWidth() + "\n");
//        System.out.print("Mezzanine.Height = " + response.getMezzanine().getHeight() + "\n");
//        System.out.print("Mezzanine.Size = " + response.getMezzanine().getSize() + "\n");
//        System.out.print("RequestId = " + response.getRequestId() + "\n");

        HashMap<String, Object> result = new HashMap<>();
        result.put("playUrl", response.getMezzanine().getFileURL());
        result.put("duration", response.getMezzanine().getDuration());
        return result;
    }
}
